//
//  MineAccountUpdateNameViewController.m
//  ShowYork
//
//  Created by hxh on 2020/8/26.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineAccountUpdateDescViewController.h"

@interface MineAccountUpdateDescViewController ()
@property (nonatomic,strong)UITextField *nameTF;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIImageView *clearImgView;
@property (nonatomic,strong)UIButton *clearButton;
@property (nonatomic,strong)UIView *line;
@end

@implementation MineAccountUpdateDescViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];

}

- (void)setUI{
    
    self.navigationItem.title = @"个性签名";
    self.view.backgroundColor = RGBA(248, 248, 248, 1);
    self.confirmBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,60, 44)];
    [self.confirmBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:RGBA(204, 204, 204, 1) forState:UIControlStateDisabled];
    [self.confirmBtn setTitleColor:C51 forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(17);
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.confirmBtn];
    
    self.navigationItem.rightBarButtonItem.enabled = NO;
    
    self.confirmBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.containView];
    
    self.nameTF = [[UITextField alloc]initWithFrame:CGRectZero];
    self.nameTF.textColor = C51;
    self.nameTF.backgroundColor = [UIColor whiteColor];
    self.nameTF.font = font(14);
    self.nameTF.placeholder = @"1-16个字";
    self.nameTF.tintColor = RGBA(245, 75, 100, 1);
    [self.nameTF addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.containView addSubview:self.nameTF];
    
    self.clearImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_black_clear_tag"]];
    [self.containView addSubview:self.clearImgView];

    self.clearButton = [[UIButton alloc]init];
    [self.clearButton addTarget:self action:@selector(clearMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.clearButton];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(17);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
        }else{
            make.top.equalTo(self.view.mas_top).offset(17);
            make.right.equalTo(self.view.mas_right);
            make.left.equalTo(self.view.mas_left);
        }
        make.height.mas_equalTo(50);
    }];
    
    
    [self.clearImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.containView);
        make.right.equalTo(self.containView).offset(-16);
    }];
    
    [self.clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.containView);
        make.left.equalTo(self.clearImgView.mas_left);
    }];
        
    [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.containView);
        make.left.equalTo(self.containView).offset(15);
        make.right.equalTo(self.clearButton.mas_left).offset(-15);
    }];
    

    
    UserInfoModel *userInfo = [UserInfoModel userInfo];
    
    self.nameTF.text = userInfo.userSign;
    
    self.navigationItem.rightBarButtonItem.enabled = self.nameTF.text.length > 0;

    self.clearButton.hidden = self.nameTF.text.length == 0;
    self.clearImgView.hidden = self.nameTF.text.length == 0;

   
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0.5);
    }];
    
    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(self.nameTF, ivar);
    placeholderLabel.textColor = RGBA(204, 204, 204, 1);
}
- (void)clearMethod{
    self.nameTF.text = @"";
    self.navigationItem.rightBarButtonItem.enabled = NO;
    self.clearButton.hidden = YES;
    self.clearImgView.hidden = YES;
}

- (void)back{
    [self.view endEditing:YES];
    [super back];
}

- (void)confirmMethod{
    [self.view endEditing:YES];
    
    if ([self.nameTF.text isSpaceString] || self.nameTF.text.length > 16) {
        [WHToast showMessage:@"请按要求设置个性签名" duration:2 finishHandler:nil];
        return;
    }
    UserInfoModel *userInfo = [UserInfoModel userInfo];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (userInfo.userLevel.intValue > 0) {
        param[@"userLevel"] = userInfo.userLevel;
    }
    param[@"sex"] = userInfo.sex;
    param[@"id"] = userInfo.id;
    param[@"userSign"] = self.nameTF.text;
    param[@"realname"] = userInfo.realName;
    param[@"imagePath"] = userInfo.imagePath;
    param[@"md5"] = [RPAppManager md5StrWithApi:UpdateUserInfo];
    [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(UpdateUserInfo) parames:param success:^(id responseObj) {
        
        userInfo.userSign = self.nameTF.text;
        
        [userInfo save];
 
        if (self.updateUserInfo) {
            self.updateUserInfo();
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(id error) {
           
    }];
}
- (void)textDidChange:(UITextField *)textField{
    if ([textField.text isSpaceString]) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }else{
        self.navigationItem.rightBarButtonItem.enabled = textField.text.length > 0;
    }
    self.clearButton.hidden = textField.text.length == 0;
    self.clearImgView.hidden = textField.text.length == 0;
    
    
    
    NSString *toBeString = textField.text;
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 16){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:16];
            if (rangeIndex.length == 1){
                textField.text = [toBeString substringToIndex:16];
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 16)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
